//
//  SymptomEmeDetailData.m
//
//  Created by lianchuang  on 14-12-12
//  Copyright (c) 2014 __MyCompanyName__. All rights reserved.
//

#import "SymptomEmeDetailData.h"
#import "SymptomEmeDetailMore.h"


NSString *const kSymptomEmeDetailDataPicurl = @"picurl";
NSString *const kSymptomEmeDetailDataPic = @"pic";
NSString *const kSymptomEmeDetailDataIscollection = @"iscollection";
NSString *const kSymptomEmeDetailDataEmeid = @"emeid";
NSString *const kSymptomEmeDetailDataMore = @"more";


@interface SymptomEmeDetailData ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation SymptomEmeDetailData

@synthesize picurl = _picurl;
@synthesize pic = _pic;
@synthesize iscollection = _iscollection;
@synthesize emeid = _emeid;
@synthesize more = _more;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.picurl = [self objectOrNilForKey:kSymptomEmeDetailDataPicurl fromDictionary:dict];
            self.pic = [self objectOrNilForKey:kSymptomEmeDetailDataPic fromDictionary:dict];
            self.iscollection = [[self objectOrNilForKey:kSymptomEmeDetailDataIscollection fromDictionary:dict] doubleValue];
            self.emeid = [[self objectOrNilForKey:kSymptomEmeDetailDataEmeid fromDictionary:dict] doubleValue];
    NSObject *receivedSymptomEmeDetailMore = [dict objectForKey:kSymptomEmeDetailDataMore];
    NSMutableArray *parsedSymptomEmeDetailMore = [NSMutableArray array];
    if ([receivedSymptomEmeDetailMore isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedSymptomEmeDetailMore) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedSymptomEmeDetailMore addObject:[SymptomEmeDetailMore modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedSymptomEmeDetailMore isKindOfClass:[NSDictionary class]]) {
       [parsedSymptomEmeDetailMore addObject:[SymptomEmeDetailMore modelObjectWithDictionary:(NSDictionary *)receivedSymptomEmeDetailMore]];
    }

    self.more = [NSArray arrayWithArray:parsedSymptomEmeDetailMore];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.picurl forKey:kSymptomEmeDetailDataPicurl];
    [mutableDict setValue:self.pic forKey:kSymptomEmeDetailDataPic];
    [mutableDict setValue:[NSNumber numberWithDouble:self.iscollection] forKey:kSymptomEmeDetailDataIscollection];
    [mutableDict setValue:[NSNumber numberWithDouble:self.emeid] forKey:kSymptomEmeDetailDataEmeid];
    NSMutableArray *tempArrayForMore = [NSMutableArray array];
    for (NSObject *subArrayObject in self.more) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForMore addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForMore addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForMore] forKey:kSymptomEmeDetailDataMore];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.picurl = [aDecoder decodeObjectForKey:kSymptomEmeDetailDataPicurl];
    self.pic = [aDecoder decodeObjectForKey:kSymptomEmeDetailDataPic];
    self.iscollection = [aDecoder decodeDoubleForKey:kSymptomEmeDetailDataIscollection];
    self.emeid = [aDecoder decodeDoubleForKey:kSymptomEmeDetailDataEmeid];
    self.more = [aDecoder decodeObjectForKey:kSymptomEmeDetailDataMore];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_picurl forKey:kSymptomEmeDetailDataPicurl];
    [aCoder encodeObject:_pic forKey:kSymptomEmeDetailDataPic];
    [aCoder encodeDouble:_iscollection forKey:kSymptomEmeDetailDataIscollection];
    [aCoder encodeDouble:_emeid forKey:kSymptomEmeDetailDataEmeid];
    [aCoder encodeObject:_more forKey:kSymptomEmeDetailDataMore];
}

- (id)copyWithZone:(NSZone *)zone
{
    SymptomEmeDetailData *copy = [[SymptomEmeDetailData alloc] init];
    
    if (copy) {

        copy.picurl = [self.picurl copyWithZone:zone];
        copy.pic = [self.pic copyWithZone:zone];
        copy.iscollection = self.iscollection;
        copy.emeid = self.emeid;
        copy.more = [self.more copyWithZone:zone];
    }
    
    return copy;
}


@end
